﻿<?php
Class Test extends CI_Model
{
	function get_test($test_id)
	{
		$this -> db -> select('test_name, time, level, level, max_score, num_question, created_user, created_date, note, original_test_id');
		$this -> db -> from('test');
		$this -> db -> where('test_id = ' . "'" . $test_id . "'");
		$this -> db -> limit(1);

		$query = $this -> db -> get();

		if($query -> num_rows() == 1)
		{
			return $query->row();
		}
		else
		{
			return false;
		}
	}
	
	function insert_test($test_name, $time, $level, $max_score, $num_question, $created_user, $created_date, $note, $original_test_id)
	{
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$data = array(
			'test_name' => $test_name,
			'time' => $time,
			'level' => $level,
			'max_score' => $max_score,
			'num_question' => $num_question,
			'created_user' => $created_user,
			'created_date' => date("Y/m/d H:i:s", time()),
			'note' => $note,
			'original_test_id' => $original_test_id
		);
		
		$this -> db -> insert('test', $data);
		
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function update_test($test_name, $time, $level, $max_score, $note, $test_id)
	{
		$data = array(
			'test_name' => $test_name,
			'time' => $time,
			'level' => $level,
			'max_score' => $max_score,
			'note' => $note
		);	
		
		$this -> db -> where('test_id', $test_id);
		$this -> db -> update('test', $data);
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	
	function insert_question_2_test($test_id, $question_id, $answer_order)
	{
		$data = array(
			'test_id' => $test_id,
			'question_id' => $question_id,
			'answer_order' => $answer_order
		);
		
		$this -> db -> insert('test_question', $data);
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;
	}
	
	function get_last_id()
	{
		return $this->db->insert_id();	
	}
	
	function delete_test($test_id)
	{
		$this->db->delete('test', array('test_id' => $test_id));
		
		if($this->db->affected_rows() == 1)
			return true;
		else
			return false;	
	}
	
	function get_total_test()
	{
		$this->db->select('*');	
		$this->db->from('test');
		
		$query = $this->db->get();
		
		if ($query)
			return $query->num_rows();
		else
			return 0;
	}
	
	function get_today_test()
	{
		$this->db->select('*');	
		$this->db->from('test');
		$this->db->where('date(created_date) = date(now())');
		
		$query = $this->db->get();
		
		if ($query)
			return $query->num_rows();
		else
			return 0;
	}
	
	function get_statistic_test()
	{
		$this->db->select('test_name, created_user, created_date');	
		$this->db->from('test');
		$this->db->limit(5);
		
		$query = $this->db->get();
		
		if ($query->num_rows() > 0)
			return $query->result();
		else
			return NULL;
	}
}